package beans;

import burp.IExtensionHelpers;
import burp.IHttpRequestResponse;
import burp.IResponseInfo;

import java.util.List;

/**
 * Created by td on 2017/6/27.
 */
public class ResponseInfo {
    private int statusCode;

    private String content_length;

    private String response_content;

    private IExtensionHelpers  helpers;

    public ResponseInfo(IExtensionHelpers helpers) {
        this.helpers = helpers;
        this.content_length = "";
        this.response_content = "";
    }

    public ResponseInfo(int statusCode, String content_length, String response_content) {
        this.statusCode = statusCode;
        this.content_length = content_length;
        this.response_content = response_content;
    }

    public int getStatusCode() {

        String s = "HTTP/1.1 200 OK\n" +
                "Cache-Control: private\n" +
                "Content-Type: text/html; charset=utf-8\n" +
                "Server: Microsoft-IIS/7.5\n" +
                "X-AspNet-Version: 4.0.30319\n" +
                "X-Powered-By: ASP.NET\n" +
                "Date: Tue, 27 Jun 2017 06:18:54 GMT\n" +
                "Connection: close\n" +
                "Content-Length: 689\n" +
                "\n" +
                "rank9940266({\"success\":true,\"message\":\"\",\"TotalCount\":\"\",\"data\":{\"idx\":\"646320\",\"zjzh\":\"2052016\",\"zhmc\":\"222å\u0095\u008Aå\u0095\u008Aå\u0095\u008A\",\"comment\":\"\",\"concerned\":\"0\",\"permit\":\"0\",\"viewCount\":\"2063\",\"initYkRate\":\"0\",\"portfRank\":\"-1\",\"portfCnt\":\"2533377\",\"indexCode\":\"49153\",\"ifConserned\":\"0\",\"holdPos\":\"0.49810\",\"portfRat\":\"0\",\"portfRatDay\":\"0.9941\",\"zzc\":\"1026554.040\",\"dealWinCnt\":\"41\",\"dealfailCnt\":\"41\",\"winCntRate\":\"0.5000\",\"balUseRat\":\"0.50000\",\"holdDayAvg\":\"0\",\"winRatDay\":\"0\",\"ykRate\":\"0.0266\",\"ykRateDay\":\"-0.0018\",\"ykRateWeek\":\"0.0474\",\"ykRateMonth\":\"0.0719\",\"ykRateYear\":\"0.0266\",\"JZ\":\"1.0270\",\"startDate\":\"20160203\",\"userid\":\"2034013776554968\",\"csje\":\"1000000.000\",\"uidNick\":\"n203413776554968\"}})";
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getContent_length() {
        return content_length;
    }

    public void setContent_length(String content_length) {
        this.content_length = content_length;
    }

    public String getResponse_content() {
        return response_content;
    }

    public void setResponse_content(String response_content) {
        this.response_content = response_content;
    }

    public ResponseInfo getResponseInfo(IHttpRequestResponse baseRequestResponse){

        IResponseInfo iResponseInfo = this.helpers.analyzeResponse(baseRequestResponse.getResponse());

        String response = this.helpers.bytesToString(baseRequestResponse.getResponse());

        int statusCode = iResponseInfo.getStatusCode();
        this.statusCode =statusCode;
        List<String> headers = iResponseInfo.getHeaders();
        String contentLength="";

        for (String string:headers){
            String[] strings = string.split(":",2);
            String type = strings[0];
            if (type.equalsIgnoreCase("content-length")){
                contentLength = strings[1];
            }
        }
        this.content_length = contentLength;

        String content = "";
        String[]  headerAndContent = response.split("\r\n\r\n",2);
        if (null!=headerAndContent[1]&&headerAndContent[1].length()>0){
            content = headerAndContent[1];
        }

        this.response_content = content;
        return this;
    }

    public  String getUnionAttriute(){

        return this.statusCode+"-"+this.content_length;
    }


}
